package com.hogae.capital.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hogae.capital.domain.CapPoolAccountRecord;
import com.hogae.capital.service.ICapPoolAccountRecordService;
import com.hogae.system.controller.SysUserController;
import com.hogae.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hogae.core.annotation.Log;
import com.hogae.core.core.controller.BaseController;
import com.hogae.core.core.domain.AjaxResult;
import com.hogae.core.enums.BusinessType;
import com.hogae.core.utils.poi.ExcelUtil;

import com.hogae.capital.domain.CapPoolAccount;
import com.hogae.capital.service.ICapPoolAccountService;
import com.hogae.core.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 资金池账户Controller
 *
 * @author hogae
 * @date 2021-02-26
 */
@Api(tags = "资金池账户")
@RestController
@RequestMapping("/capital/poolAccount")
public class CapPoolAccountController extends BaseController {
    @Autowired
    private ICapPoolAccountService capPoolAccountService;

    @Autowired
    private ICapPoolAccountRecordService capPoolAccountRecordService;

    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 查询资金池账户列表
     */
    @ApiOperation("查询资金池账户列表")
    @PreAuthorize("@ss.hasPermi('capital:poolAccount:list')")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public TableDataInfo list(CapPoolAccount capPoolAccount) {
        startPage();
        List<CapPoolAccount> list = capPoolAccountService.selectCapPoolAccountList(capPoolAccount);
        return getDataTable(list);
    }

    /**
     * 导出资金池账户列表
     */
    @ApiOperation("导出资金池账户列表")
    @PreAuthorize("@ss.hasPermi('capital:poolAccount:export')")
    @Log(title = "资金池账户", businessType = BusinessType.EXPORT)
    @GetMapping(value = "/export", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult export(CapPoolAccount capPoolAccount) {
        List<CapPoolAccount> list = capPoolAccountService.selectCapPoolAccountList(capPoolAccount);
        ExcelUtil<CapPoolAccount> util = new ExcelUtil<CapPoolAccount>(CapPoolAccount.class);
        return util.exportExcel(list, "poolAccount");
    }

    /**
     * 获取资金池账户详细信息
     */
    @ApiOperation("获取资金池账户详细信息")
    @PreAuthorize("@ss.hasPermi('capital:poolAccount:query')")
    @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(capPoolAccountService.selectCapPoolAccountById(id));
    }

    /**
     * 新增资金池账户
     */
    @ApiOperation("新增资金池账户")
    @PreAuthorize("@ss.hasPermi('capital:poolAccount:add')")
    @Log(title = "资金池账户", businessType = BusinessType.INSERT)
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult add(@RequestBody CapPoolAccount capPoolAccount) {
        return toAjax(capPoolAccountService.insertCapPoolAccount(capPoolAccount));
    }

    /**
     * 修改资金池账户
     */
    @ApiOperation("修改资金池账户")
    @PreAuthorize("@ss.hasPermi('capital:poolAccount:edit')")
    @Log(title = "资金池账户", businessType = BusinessType.UPDATE)
    @PutMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult edit(@RequestBody CapPoolAccount capPoolAccount) {
        return toAjax(capPoolAccountService.updateCapPoolAccount(capPoolAccount));
    }

    /**
     * 删除资金池账户
     */
    @ApiOperation("删除资金池账户")
    @PreAuthorize("@ss.hasPermi('capital:poolAccount:remove')")
    @Log(title = "资金池账户", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/{ids}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(capPoolAccountService.deleteCapPoolAccountByIds(ids));
    }

    @Transactional
    @ApiOperation("资金池账户充值")
    @Log(title = "资金池记录", businessType = BusinessType.UPDATE)
    @PostMapping("/capitalPoolRecharge")
    public AjaxResult capitalPoolRecharge(@RequestBody CapPoolAccountRecord capPoolAccountRecord) {
        int i = capPoolAccountRecordService.insertCapPoolAccountRecord(capPoolAccountRecord);
        if (i == 1) {
            CapPoolAccount sqlCapPoolAccount = capPoolAccountService.selectCapPoolAccountById(capPoolAccountRecord.getFromUserId());
            sqlCapPoolAccount.setAmount(BigDecimal.valueOf(Double.valueOf(capPoolAccountRecord.getAmount().toString()) + Double.valueOf(sqlCapPoolAccount.getAmount().toString())));
            return toAjax(capPoolAccountService.updateCapPoolAccount(sqlCapPoolAccount));
        }
        return toAjax(i);
    }


    @ApiOperation("资金池账户支出")
    @Log(title = "资金池记录", businessType = BusinessType.UPDATE)
    @PostMapping("/fundPoolAllocation")
    public AjaxResult fundPoolAllocation(@RequestBody CapPoolAccountRecord capPoolAccountRecord) {
        int i = capPoolAccountRecordService.insertCapPoolAccountRecord(capPoolAccountRecord);
        if (i == 1) {
            CapPoolAccount sqlCapPoolAccount = capPoolAccountService.selectCapPoolAccountById(capPoolAccountRecord.getFromUserId());
            sqlCapPoolAccount.setAmount(BigDecimal.valueOf(Double.valueOf(sqlCapPoolAccount.getAmount().toString()) - Double.valueOf(capPoolAccountRecord.getAmount().toString())));
            return toAjax(capPoolAccountService.updateCapPoolAccount(sqlCapPoolAccount));
        }
        return toAjax(i);
    }


    @ApiOperation("资金池充值统计")
    @PostMapping("/fundPoolStatistics")
    public AjaxResult fundPoolStatistics(CapPoolAccount capPoolAccount) {
        CapPoolAccount capPoolAccount1 = capPoolAccountService.selectCapPoolAccountById(capPoolAccount.getUserId());
        Map<Object, Object> map = new HashMap<>();
        CapPoolAccountRecord capPoolAccountRecord1 = new CapPoolAccountRecord();
        capPoolAccountRecord1.setPoolOperationType("0");
        capPoolAccountRecord1.setFromUserId(capPoolAccount.getUserId());
        List<CapPoolAccountRecord> capPoolAccountRecordList = capPoolAccountRecordService.selectByFromUserIdAndAmountType(capPoolAccountRecord1);
        Double totalRecharge = 0.0;
        for (CapPoolAccountRecord capPoolAccountRecord : capPoolAccountRecordList) {
            totalRecharge += capPoolAccountRecord.getAmount().doubleValue();
        }
        map.put("existing", capPoolAccount1.getAmount());
        map.put("totalRecharge", totalRecharge);
        return AjaxResult.success(map);
    }


    @ApiOperation("资金池账户禁用")
    @Log(title = "资金池账户禁用", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/editStatus")
    public AjaxResult editStatus(@RequestBody CapPoolAccount capPoolAccount) {
        return toAjax(capPoolAccountService.updateCapPoolAccountStatus(capPoolAccount));
    }

    @ApiOperation("获取当前用户的下级id")
    @Log(title = "获取当前用户的下级id")
    @GetMapping(value = "/{userId}")
    public AjaxResult toCapPoolList(@PathVariable Long userId) {

        List<Integer> integers = iSysUserService.selectByParentUserId(userId);
        return AjaxResult.success(capPoolAccountService.selectByUserId(integers));
    }
}
